package com.ruoyi.income.service.impl;

import java.util.List;
import java.math.BigDecimal;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.income.mapper.DistributionIncomeMapper;
import com.ruoyi.income.domain.DistributionIncome;
import com.ruoyi.income.service.IDistributionIncomeService;

/**
 * 订单返佣收入信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-11
 */
@Service
public class DistributionIncomeServiceImpl implements IDistributionIncomeService
{
    @Autowired
    private DistributionIncomeMapper distributionIncomeMapper;

    /**
     * 查询订单返佣收入信息
     *
     * @param incomeId 订单返佣收入信息主键
     * @return 订单返佣收入信息
     */
    @Override
    public DistributionIncome selectDistributionIncomeByIncomeId(Long incomeId)
    {
        return distributionIncomeMapper.selectDistributionIncomeByIncomeId(incomeId);
    }

    /**
     * 查询订单返佣收入信息列表
     *
     * @param distributionIncome 订单返佣收入信息
     * @return 订单返佣收入信息
     */
    @Override
    public List<DistributionIncome> selectDistributionIncomeList(DistributionIncome distributionIncome)
    {
        return distributionIncomeMapper.selectDistributionIncomeList(distributionIncome);
    }

    /**
     * 新增订单返佣收入信息
     *
     * @param distributionIncome 订单返佣收入信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDistributionIncome(DistributionIncome distributionIncome)
    {
        // 参数校验
        validateDistributionIncome(distributionIncome);

        distributionIncome.setCreateTime(DateUtils.getNowDate());
//        distributionIncome.setCreateBy(SecurityUtils.getUsername());

        // 设置默认状态
        if (distributionIncome.getIncomeStatus() == null) {
            distributionIncome.setIncomeStatus("0"); // 待计算
        }

        return distributionIncomeMapper.insertDistributionIncome(distributionIncome);
    }

    /**
     * 修改订单返佣收入信息
     *
     * @param distributionIncome 订单返佣收入信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDistributionIncome(DistributionIncome distributionIncome)
    {
        // 参数校验
        validateDistributionIncome(distributionIncome);

//        distributionIncome.setUpdateTime(DateUtils.getNowDate());
//        distributionIncome.setUpdateBy(SecurityUtils.getUsername());

        return distributionIncomeMapper.updateDistributionIncome(distributionIncome);
    }

    /**
     * 批量删除订单返佣收入信息
     *
     * @param incomeIds 需要删除的订单返佣收入信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDistributionIncomeByIncomeIds(Long[] incomeIds)
    {
        return distributionIncomeMapper.deleteDistributionIncomeByIncomeIds(incomeIds);
    }

    /**
     * 删除订单返佣收入信息信息
     *
     * @param incomeId 订单返佣收入信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDistributionIncomeByIncomeId(Long incomeId)
    {
        return distributionIncomeMapper.deleteDistributionIncomeByIncomeId(incomeId);
    }

    /**
     * 验证订单返佣收入信息参数
     */
    private void validateDistributionIncome(DistributionIncome distributionIncome) {
        if (distributionIncome == null) {
            throw new IllegalArgumentException("订单返佣收入信息不能为空");
        }

        if (distributionIncome.getOrderId() == null) {
            throw new IllegalArgumentException("订单ID不能为空");
        }

        if (distributionIncome.getDistributorId() == null) {
            throw new IllegalArgumentException("分销员ID不能为空");
        }

        if (distributionIncome.getRebateAmount() != null &&
            distributionIncome.getRebateAmount().compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("返佣金额不能为负数");
        }
    }

    /**
     * 更新佣金状态
     */
    @Override
    @Transactional
    public int updateIncomeStatus(Long incomeId, String incomeStatus) {
        DistributionIncome income = new DistributionIncome();
        income.setIncomeId(incomeId);
        income.setIncomeStatus(incomeStatus);

        if ("2".equals(incomeStatus)) { // 已发放
            income.setIssueTime(DateUtils.getNowDate());
        } else if ("1".equals(incomeStatus)) { // 已计算
            income.setCalculateTime(DateUtils.getNowDate());
        }

        income.setUpdateTime(DateUtils.getNowDate());
        income.setUpdateBy(SecurityUtils.getUsername());

        return distributionIncomeMapper.updateDistributionIncome(income);
    }

    /**
     * 根据分销员ID查询收入列表
     */
    @Override
    public List<DistributionIncome> selectIncomeByDistributorId(Long distributorId) {
        DistributionIncome query = new DistributionIncome();
        query.setDistributorId(distributorId);
        return distributionIncomeMapper.selectDistributionIncomeList(query);
    }

    /**
     * 根据订单ID和分销员ID查询收入信息
     */
    @Override
    public DistributionIncome selectIncomeByOrderIdAndDistributorId(Long orderId, Long distributorId) {
        return distributionIncomeMapper.selectIncomeByOrderIdAndDistributorId(orderId, distributorId);
    }
}
